home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / sstring.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  10.1 KB  |  459 lines

  1. /* Copyright (C) 1993, 1995, 1996, 1997, 1998, 1999 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: sstring.c,v 1.2 2000/09/19 19:00:51 lpd Exp $ */
  20. /* String and hexstring streams (filters) */
  21. #include "stdio_.h"        /* includes std.h */
  22. #include "memory_.h"
  23. #include "string_.h"
  24. #include "strimpl.h"
  25. #include "sstring.h"
  26. #include "scanchar.h"
  27.  
  28. /* ------ ASCIIHexEncode ------ */
  29.  
  30. private_st_AXE_state();
  31.  
  32. /* Initialize the state */
  33. private int
  34. s_AXE_init(stream_state * st)
  35. {
  36.     stream_AXE_state *const ss = (stream_AXE_state *) st;
  37.  
  38.     return s_AXE_init_inline(ss);
  39. }
  40.  
  41. /* Process a buffer */
  42. private int
  43. s_AXE_process(stream_state * st, stream_cursor_read * pr,
  44.           stream_cursor_write * pw, bool last)
  45. {
  46.     stream_AXE_state *const ss = (stream_AXE_state *) st;
  47.     const byte *p = pr->ptr;
  48.     byte *q = pw->ptr;
  49.     int rcount = pr->limit - p;
  50.     int wcount = pw->limit - q;
  51.     int count;
  52.     int pos = ss->count;
  53.     const char *hex_digits = "0123456789ABCDEF";
  54.     int status = 0;
  55.  
  56.     if (last && ss->EndOfData)
  57.     wcount--;        /* leave room for '>' */
  58.     wcount -= (wcount + 64) / 65;    /* leave room for \n */
  59.     wcount >>= 1;        /* 2 chars per input byte */
  60.     count = (wcount < rcount ? (status = 1, wcount) : rcount);
  61.     while (--count >= 0) {
  62.     *++q = hex_digits[*++p >> 4];
  63.     *++q = hex_digits[*p & 0xf];
  64.     if (!(++pos & 31) && (count != 0 || !last))
  65.         *++q = '\n';
  66.     }
  67.     if (last && status == 0 && ss->EndOfData)
  68.     *++q = '>';
  69.     pr->ptr = p;
  70.     pw->ptr = q;
  71.     ss->count = pos & 31;
  72.     return status;
  73. }
  74.  
  75. /* Stream template */
  76. const stream_template s_AXE_template =
  77. {&st_AXE_state, s_AXE_init, s_AXE_process, 1, 3
  78. };
  79.  
  80. /* ------ ASCIIHexDecode ------ */
  81.  
  82. private_st_AXD_state();
  83.  
  84. /* Initialize the state */
  85. private int
  86. s_AXD_init(stream_state * st)
  87. {
  88.     stream_AXD_state *const ss = (stream_AXD_state *) st;
  89.  
  90.     return s_AXD_init_inline(ss);
  91. }
  92.  
  93. /* Process a buffer */
  94. private int
  95. s_AXD_process(stream_state * st, stream_cursor_read * pr,
  96.           stream_cursor_write * pw, bool last)
  97. {
  98.     stream_AXD_state *const ss = (stream_AXD_state *) st;
  99.     int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
  100.  
  101.     switch (code) {
  102.     case 0:
  103.         if (ss->odd >= 0 && last) {
  104.         if (pw->ptr == pw->limit)
  105.             return 1;
  106.         *++(pw->ptr) = ss->odd << 4;
  107.         }
  108.         /* falls through */
  109.     case 1:
  110.         /* We still need to read ahead and check for EOD. */
  111.         for (; pr->ptr < pr->limit; pr->ptr++)
  112.         if (scan_char_decoder[pr->ptr[1]] != ctype_space) {
  113.             if (pr->ptr[1] == '>') {
  114.             pr->ptr++;
  115.             goto eod;
  116.             }
  117.             return 1;
  118.         }
  119.         return 0;        /* still need to scan ahead */
  120.     default:
  121.         return code;
  122.     case ERRC:
  123.         ;
  124.     }
  125.     /*
  126.      * Check for EOD.  ERRC implies at least one more character
  127.      * was read; we must unread it, since the caller might have
  128.      * invoked the filter with exactly the right count to read all
  129.      * the available data, and we might be reading past the end.
  130.      */
  131.     if (*pr->ptr != '>') {    /* EOD */
  132.     --(pr->ptr);
  133.     return ERRC;
  134.     }
  135.   eod:if (ss->odd >= 0) {
  136.     if (pw->ptr == pw->limit)
  137.         return 1;
  138.     *++(pw->ptr) = ss->odd << 4;
  139.     }
  140.     return EOFC;
  141. }
  142.  
  143. /* Stream template */
  144. const stream_template s_AXD_template =
  145. {&st_AXD_state, s_AXD_init, s_AXD_process, 2, 1
  146. };
  147.  
  148. /* ------ PSStringEncode ------ */
  149.  
  150. /* Process a buffer */
  151. private int
  152. s_PSSE_process(stream_state * st, stream_cursor_read * pr,
  153.            stream_cursor_write * pw, bool last)
  154. {
  155.     const byte *p = pr->ptr;
  156.     const byte *rlimit = pr->limit;
  157.     byte *q = pw->ptr;
  158.     byte *wlimit = pw->limit;
  159.     int status = 0;
  160.  
  161.     /* This doesn't have to be very efficient. */
  162.     while (p < rlimit) {
  163.     int c = *++p;
  164.  
  165.     if (c < 32 || c >= 127) {
  166.         const char *pesc;
  167.         const char *const esc = "\n\r\t\b\f";
  168.  
  169.         if (c < 32 && c != 0 && (pesc = strchr(esc, c)) != 0) {
  170.         if (wlimit - q < 2) {
  171.             --p;
  172.             status = 1;
  173.             break;
  174.         }
  175.         *++q = '\\';
  176.         *++q = "nrtbf"[pesc - esc];
  177.         continue;
  178.         }
  179.         if (wlimit - q < 4) {
  180.         --p;
  181.         status = 1;
  182.         break;
  183.         }
  184.         q[1] = '\\';
  185.         q[2] = (c >> 6) + '0';
  186.         q[3] = ((c >> 3) & 7) + '0';
  187.         q[4] = (c & 7) + '0';
  188.         q += 4;
  189.         continue;
  190.     } else if (c == '(' || c == ')' || c == '\\') {
  191.         if (wlimit - q < 2) {
  192.         --p;
  193.         status = 1;
  194.         break;
  195.         }
  196.         *++q = '\\';
  197.     } else {
  198.         if (q == wlimit) {
  199.         --p;
  200.         status = 1;
  201.         break;
  202.         }
  203.     }
  204.     *++q = c;
  205.     }
  206.     if (last && status == 0) {
  207.     if (q == wlimit)
  208.         status = 1;
  209.     else
  210.         *++q = ')';
  211.     }
  212.     pr->ptr = p;
  213.     pw->ptr = q;
  214.     return status;
  215. }
  216.  
  217. /* Stream template */
  218. const stream_template s_PSSE_template =
  219. {&st_stream_state, NULL, s_PSSE_process, 1, 4
  220. };
  221.  
  222. /* ------ PSStringDecode ------ */
  223.  
  224. private_st_PSSD_state();
  225.  
  226. /* Initialize the state */
  227. private int
  228. s_PSSD_init(stream_state * st)
  229. {
  230.     stream_PSSD_state *const ss = (stream_PSSD_state *) st;
  231.  
  232.     return s_PSSD_init_inline(ss);
  233. }
  234.  
  235. /* Process a buffer */
  236. private int
  237. s_PSSD_process(stream_state * st, stream_cursor_read * pr,
  238.            stream_cursor_write * pw, bool last)
  239. {
  240.     stream_PSSD_state *const ss = (stream_PSSD_state *) st;
  241.     const byte *p = pr->ptr;
  242.     const byte *rlimit = pr->limit;
  243.     byte *q = pw->ptr;
  244.     byte *wlimit = pw->limit;
  245.     int status = 0;
  246.     int c;
  247.  
  248. #define check_p(n)\
  249.   if ( p == rlimit ) { p -= n; goto out; }
  250. #define check_q(n)\
  251.   if ( q == wlimit ) { p -= n; status = 1; goto out; }
  252.     while (p < rlimit) {
  253.     c = *++p;
  254.     if (c == '\\' && !ss->from_string) {
  255.         check_p(1);
  256.         switch ((c = *++p)) {
  257.         case 'n':
  258.             c = '\n';
  259.             goto put;
  260.         case 'r':
  261.             c = '\r';
  262.             goto put;
  263.         case 't':
  264.             c = '\t';
  265.             goto put;
  266.         case 'b':
  267.             c = '\b';
  268.             goto put;
  269.         case 'f':
  270.             c = '\f';
  271.             goto put;
  272.         default:    /* ignore the \ */
  273.           put:check_q(2);
  274.             *++q = c;
  275.             continue;
  276.         case char_CR:    /* ignore, check for following \n */
  277.             check_p(2);
  278.             if (p[1] == char_EOL)
  279.             p++;
  280.             continue;
  281.         case char_EOL:    /* ignore */
  282.             continue;
  283.         case '0':
  284.         case '1':
  285.         case '2':
  286.         case '3':
  287.         case '4':
  288.         case '5':
  289.         case '6':
  290.         case '7':
  291.             {
  292.             int d;
  293.  
  294.             check_p(2);
  295.             d = p[1];
  296.             c -= '0';
  297.             if (d >= '0' && d <= '7') {
  298.                 if (p + 1 == rlimit) {
  299.                 p -= 2;
  300.                 goto out;
  301.                 }
  302.                 check_q(2);
  303.                 c = (c << 3) + d - '0';
  304.                 d = p[2];
  305.                 if (d >= '0' && d <= '7') {
  306.                 c = (c << 3) + d - '0';
  307.                 p += 2;
  308.                 } else
  309.                 p++;
  310.             } else
  311.                 check_q(2);
  312.             *++q = c;
  313.             continue;
  314.             }
  315.         }
  316.     } else
  317.         switch (c) {
  318.         case '(':
  319.             check_q(1);
  320.             ss->depth++;
  321.             break;
  322.         case ')':
  323.             if (ss->depth == 0) {
  324.             status = EOFC;
  325.             goto out;
  326.             }
  327.             check_q(1);
  328.             ss->depth--;
  329.             break;
  330.         case char_CR:    /* convert to \n */
  331.             check_p(1);
  332.             check_q(1);
  333.             if (p[1] == char_EOL)
  334.             p++;
  335.             *++q = '\n';
  336.             continue;
  337.         case char_EOL:
  338.             c = '\n';
  339.         default:
  340.             check_q(1);
  341.             break;
  342.         }
  343.     *++q = c;
  344.     }
  345. #undef check_p
  346. #undef check_q
  347.   out:pr->ptr = p;
  348.     pw->ptr = q;
  349.     if (last && status == 0 && p != rlimit)
  350.     status = ERRC;
  351.     return status;
  352. }
  353.  
  354. /* Stream template */
  355. const stream_template s_PSSD_template =
  356. {&st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1
  357. };
  358.  
  359. /* ------ Utilities ------ */
  360.  
  361. /*
  362.  * Convert hex data to binary.  Return 1 if we filled the string, 0 if
  363.  * we ran out of input data before filling the string, or ERRC on error.
  364.  * The caller must set *odd_digit to -1 before the first call;
  365.  * after each call, if an odd number of hex digits has been read (total),
  366.  * *odd_digit is the odd digit value, otherwise *odd_digit = -1.
  367.  * See strimpl.h for the definition of syntax.
  368.  */
  369. int
  370. s_hex_process(stream_cursor_read * pr, stream_cursor_write * pw,
  371.           int *odd_digit, hex_syntax syntax)
  372. {
  373.     const byte *p = pr->ptr;
  374.     const byte *rlimit = pr->limit;
  375.     byte *q = pw->ptr;
  376.     byte *wlimit = pw->limit;
  377.     byte *q0 = q;
  378.     byte val1 = (byte) * odd_digit;
  379.     byte val2;
  380.     uint rcount;
  381.     byte *flimit;
  382.     const byte *const decoder = scan_char_decoder;
  383.     int code = 0;
  384.  
  385.     if (q >= wlimit)
  386.     return 1;
  387.     if (val1 <= 0xf)
  388.     goto d2;
  389.   d1:if ((rcount = (rlimit - p) >> 1) == 0)
  390.     goto x1;
  391.     /* Set up a fast end-of-loop check, so we don't have to test */
  392.     /* both p and q against their respective limits. */
  393.     flimit = (rcount < wlimit - q ? q + rcount : wlimit);
  394.   f1:if ((val1 = decoder[p[1]]) <= 0xf &&
  395.     (val2 = decoder[p[2]]) <= 0xf
  396.     ) {
  397.     p += 2;
  398.     *++q = (val1 << 4) + val2;
  399.     if (q < flimit)
  400.         goto f1;
  401.     if (q >= wlimit)
  402.         goto px;
  403.     }
  404.   x1:if (p >= rlimit)
  405.     goto end1;
  406.     if ((val1 = decoder[*++p]) > 0xf) {
  407.     if (val1 == ctype_space) {
  408.         switch (syntax) {
  409.         case hex_ignore_whitespace:
  410.             goto x1;
  411.         case hex_ignore_leading_whitespace:
  412.             if (q == q0 && *odd_digit < 0)
  413.             goto x1;
  414.             --p;
  415.             code = 1;
  416.             goto end1;
  417.         case hex_ignore_garbage:
  418.             goto x1;
  419.         }
  420.     } else if (syntax == hex_ignore_garbage)
  421.         goto x1;
  422.     code = ERRC;
  423.     goto end1;
  424.     }
  425.   d2:if (p >= rlimit) {
  426.     *odd_digit = val1;
  427.     goto ended;
  428.     }
  429.     if ((val2 = decoder[*++p]) > 0xf) {
  430.     if (val2 == ctype_space)
  431.         switch (syntax) {
  432.         case hex_ignore_whitespace:
  433.             goto d2;
  434.         case hex_ignore_leading_whitespace:
  435.             if (q == q0)
  436.             goto d2;
  437.             --p;
  438.             *odd_digit = val1;
  439.             code = 1;
  440.             goto ended;
  441.         case hex_ignore_garbage:    /* pacify compilers */
  442.             ;
  443.         }
  444.     if (syntax == hex_ignore_garbage)
  445.         goto d2;
  446.     *odd_digit = val1;
  447.     code = ERRC;
  448.     goto ended;
  449.     }
  450.     *++q = (val1 << 4) + val2;
  451.     if (q < wlimit)
  452.     goto d1;
  453.   px:code = 1;
  454.   end1:*odd_digit = -1;
  455.   ended:pr->ptr = p;
  456.     pw->ptr = q;
  457.     return code;
  458. }
  459.